/*
 * @Description: 要货计划DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-12-29 18:00:30
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 15:12:11
 */

import { DataSetSelection, FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';
import intl from 'utils/intl';
// import { DataSet } from 'choerodon-ui/pro/lib';
import moment from 'moment';
import { resetMonth } from '@src/utils/utilsFun';
// const O2_CM = process.env.NODE_ENV === 'development' ? '/o2cp' : commonConfigStore.O2_CM;
const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;

// const isOption = new DataSet({
//     selection: 'single' as DataSetSelection,
//     data: [
//         { text: '显示', value: 1 },
//         { text: '不显示', value: 0 },
//     ],
// });

// 区域DS
const areaDateSet = () => ({
    primaryKey: 'keyId',
    selection: 'single' as DataSetSelection,
    autoQuery: true,
    paging: false,
    parentField: 'largeAreaId',
    idField: 'keyId',
    expandField: 'expand',
    checkField: 'check',
    transport: {
        read: (config) => {
            return {
                ...config,
                url: `/hitf/v1/dji-external-interface/get-regions`,
                method: 'post',
                transformResponse: value => {
                    const reqData = JSON.parse(value);
                    const reqareaList = reqData.data
                    const largeList: any = []
                    const areaList: any = []
                    // 去重obj
                    let obj = {}

                    // 去重多个小区
                    let objArea = {}
                    let id = 100
                    reqareaList.forEach(item => {
                        const key = item.largeAreaId
                        if (!obj[key] && item.largeAreaId) {
                            largeList.push({
                                largeAreaCnName: item.largeAreaCnName,
                                areaId: item.largeAreaId,
                                largeAreaData: true,
                                keyId: item.largeAreaId,
                            })
                            obj[key] = key;
                        }

                        const key2 = item.areaId 
                        if (!objArea[key2]) {
                            largeList.push({
                                ...item,
                                keyId: id++
                            })
                            objArea[key2] = key2;
                        }
                    })

                    const showAreaTreeList = areaList.concat(largeList)


                    // 解决大区域没有 regionName regionId显示不了的问题
                    // const newList = reqData.map(item => {
                    //     return {
                    //         ...item,
                    //         regionName: item.regionName || item.areaName,
                    //         regionId: item.regionId || item.areaId,
                    //     };
                    // });
                    // // 按名字字母进行排序
                    // newList.sort((a, b) => { return a.regionName.charCodeAt(0) - b.regionName.charCodeAt(0); });
                    return showAreaTreeList;
                },
            };
        },
    },
});

// 编辑 查看 DS
const detailDS = () => ({
    autoCreate: true,
    dataToJSON: 'normal',
    paging: false,
    transport: {
        read: config => {
            const { data } = config
            return {
                ...config,
                url: `${O2_OM}/v1/requirement-tasks/${data.taskId}`,
                data: [],
                method: 'get',
            };
        },
    },
});
// 要货任务列表DS
const requireGoodsPlanDS = () => ({
    autoQuery: true,
    selection: 'multiple',
    primaryKey: 'id',
    transport: {
        read: config => {
            // const { data } = config;
            // console.log(data);

            // const { endDate, startDate } = data;
            // // 截掉月份后面的时间
            // let submitData = {...data};
            // if (startDate || endDate) {
            //     const start = startDate ? startDate.substring(0, 10) : null;
            //     const end = endDate? endDate.substring(0, 10) : null;
            //     submitData = {
            //         ...data,
            //         endDate: end,
            //         startDate: start
            //     };
            // } 
            return {
                ...config,
                // data: {
                //     ...submitData,
                // },
                url: `${O2_OM}/v1/requirement-tasks`,
                method: 'get',
            };
        },
        create: config => {
            const data = config.data[0];
            const { taskMonth } = data;
            // 截掉月份后面的时间
            const submitData = resetMonth(data, taskMonth, 'taskMonth')
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url: `${O2_OM}/v1/requirement-tasks`,
                method: 'post',
            };
        },
        update: config => {
            const data = config.data[0];
            const { taskMonth } = data;
            // 截掉月份后面的时间
            // 截掉月份后面的时间
            const submitData = resetMonth(data, taskMonth, 'taskMonth')
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url: `${O2_OM}/v1/requirement-tasks`,
                method: 'put',
            };
        },
        destroy: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/requirement-tasks`,
                method: 'delete',
            };
        },
    },
    fields: [
        {
            name: 'taskCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.taskCode').d('任务编码'),
        },
        {
            name: 'taskDescription',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.taskDescription').d('任务名称'),
            required: true,
        },
        {
            name: 'taskMonth',
            type: FieldType.month,
            label: intl.get('dji.otc.om.view.field.taskMonth').d('要货月份'),
            required: true,
            defaultValue: moment().format("YYYY MM"),
        },
        {
            name: 'taskStatusCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.taskStatusCode').d('任务状态'),
            lookupCode: 'DJISM.TASK_STATUS',
        },
        {
            name: 'startDate',
            type: FieldType.date,
            label: intl.get('dji.otc.om.view.field.startDate').d('开始日期'),
            required: true,
            defaultValue: moment().format("YYYY MM DD"),
        },
        {
            name: 'endDate',
            type: FieldType.date,
            label: intl.get('dji.otc.om.view.field.endDate').d('结束日期'),
            required: true,
        },
        {
            name: 'isShowDealer',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.isShowDealer').d('是否代理商可见'),
            lookupCode: 'DJIOM.CUSTOMER_ORDER_DISPLAY',
            required: true,
            defaultValue: 1,
        },
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
            lookupCode: 'DJICM_GROUP',
            required: true,
        },
        {
            name: 'createdBy',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.createdUserName').d('创建人'),
        },
        {
            name: 'creationDate',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.creationDate').d('创建时间'),
        },
        {
            name: 'djiRequirementTaskAreas',
            type: FieldType.object,
            // label: intl.get('dji.otc.om.view.field.djiRequirementTaskAreas').d('区域'),
            required: true,
        },
        {
            name: 'showAreaLabel',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.showAreaLabel').d('区域'),
            // required: true,
            ignore: 'always'
        },
    ],
    queryFields: [
        {
            name: 'taskCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.taskCode').d('任务编码'),
        },
        {
            name: 'taskDescription',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.taskDescription').d('任务名称'),
        },
        {
            name: 'dateObj',
            type: FieldType.date,
            label: intl.get('dji.otc.om.view.field.taskTime').d('任务时间'),
            range: ['start', 'end'],
            ignore: 'always',
        },
        {
            label: intl.get('dji.otc.om.view.field.effectiveDate').d('生效日期'),
            name: 'startDate',
            type: FieldType.date,
            bind: 'dateObj.start',
        },
        {
            label: intl.get('dji.otc.om.view.field.invalidDate').d('失效日期'),
            name: 'endDate',
            type: FieldType.date,
            bind: 'dateObj.end',
        },
    ],
});

// 发布 撤回ds
const publishDS = () => ({
    autoCreate: true,
    dataToJSON: 'normal',
    transport: {
        submit: config => {
            return {
                ...config,
                data: config.data[0],
                url: `${O2_OM}/v1/requirement-tasks/publish`,
                method: 'POST',
            };
        },
    },
});

export { requireGoodsPlanDS, publishDS, areaDateSet, detailDS };
